package com.sky.skyopsserver.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sky.skyopsserver.aspect.ApiOperationLog;
import com.sky.skyopsserver.service.ComputerRoomService;
import com.sky.skyopsserver.utils.PageResponse;
import com.sky.skyopsserver.utils.Response;
import com.sky.skyopsserver.vos.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机房信息控制器
 * 
 * @author Administrator
 * @since 2025-01-17
 */
@Slf4j
@RestController
@RequestMapping("/api/computer-room")
@RequiredArgsConstructor
@Validated
@Tag(name = "机房管理", description = "机房信息的增删改查操作")
public class ComputerRoomController {

    private final ComputerRoomService computerRoomService;

    /**
     * 添加机房
     * 
     * @param reqVO 添加机房请求VO
     * @return 机房ID
     */
    @PostMapping("/add")
    @Operation(summary = "添加机房", description = "新增一个机房信息")
    @ApiOperationLog(description = "添加机房")
    public Response<Long> addComputerRoom(@Valid @RequestBody ComputerRoomAddReqVO reqVO) {
        Long roomId = computerRoomService.addComputerRoom(reqVO);
        return Response.success(roomId);
    }

    /**
     * 更新机房信息
     * 
     * @param reqVO 更新机房请求VO
     * @return 操作结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新机房信息", description = "修改机房的基本信息")
    @ApiOperationLog(description = "更新机房信息")
    public Response<Boolean> updateComputerRoom(@Valid @RequestBody ComputerRoomUpdateReqVO reqVO) {
        Boolean result = computerRoomService.updateComputerRoom(reqVO);
        return Response.success(result);
    }

    /**
     * 删除机房
     * 
     * @param id 机房ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除机房", description = "根据ID删除指定机房")
    @ApiOperationLog(description = "删除机房")
    public Response<Boolean> deleteComputerRoom(
            @Parameter(description = "机房ID", required = true)
            @PathVariable("id") @NotNull(message = "机房ID不能为空") @Positive(message = "机房ID必须为正数") Long id) {
        Boolean result = computerRoomService.deleteComputerRoom(id);
        return Response.success(result);
    }

    /**
     * 批量删除机房
     * 
     * @param ids 机房ID列表
     * @return 操作结果
     */
    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除机房", description = "根据ID列表批量删除机房")
    @ApiOperationLog(description = "批量删除机房")
    public Response<Boolean> batchDeleteComputerRoom(
            @Parameter(description = "机房ID列表", required = true)
            @RequestBody @NotEmpty(message = "机房ID列表不能为空") List<@Positive(message = "机房ID必须为正数") Long> ids) {
        Boolean result = computerRoomService.batchDeleteComputerRoom(ids);
        return Response.success(result);
    }

    /**
     * 根据ID查询机房详情
     * 
     * @param id 机房ID
     * @return 机房详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "查询机房详情", description = "根据ID获取机房的详细信息")
    @ApiOperationLog(description = "查询机房详情")
    public Response<ComputerRoomRspVO> getComputerRoomById(
            @Parameter(description = "机房ID", required = true)
            @PathVariable("id") @NotNull(message = "机房ID不能为空") @Positive(message = "机房ID必须为正数") Long id) {
        ComputerRoomRspVO result = computerRoomService.getComputerRoomById(id);
        return Response.success(result);
    }

    /**
     * 分页查询机房列表
     * 
     * @param reqVO 分页查询请求VO
     * @return 分页结果
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询机房列表", description = "根据条件分页查询机房信息")
    @ApiOperationLog(description = "分页查询机房列表")
    public PageResponse<ComputerRoomRspVO> getComputerRoomPage(@Valid @RequestBody ComputerRoomPageReqVO reqVO) {
        IPage<ComputerRoomRspVO> page = computerRoomService.getComputerRoomPage(reqVO);
        return PageResponse.success(page, page.getRecords());
    }

    /**
     * 查询所有机房选项（用于下拉选择）
     * 
     * @return 机房选项列表
     */
    @GetMapping("/options")
    @Operation(summary = "查询机房选项", description = "获取所有机房的选项列表，用于下拉选择")
    @ApiOperationLog(description = "查询机房选项")
    public Response<List<ComputerRoomOptionRspVO>> getAllComputerRoomOptions() {
        List<ComputerRoomOptionRspVO> result = computerRoomService.getAllComputerRoomOptions();
        return Response.success(result);
    }

    /**
     * 根据城市查询机房选项
     * 
     * @param city 城市名称
     * @return 机房选项列表
     */
    @GetMapping("/options/city/{city}")
    @Operation(summary = "根据城市查询机房选项", description = "根据指定城市获取机房选项列表")
    @ApiOperationLog(description = "根据城市查询机房选项")
    public Response<List<ComputerRoomOptionRspVO>> getComputerRoomOptionsByCity(
            @Parameter(description = "城市名称", required = true)
            @PathVariable("city") String city) {
        List<ComputerRoomOptionRspVO> result = computerRoomService.getComputerRoomOptionsByCity(city);
        return Response.success(result);
    }

    /**
     * 根据运营商查询机房选项
     * 
     * @param operator 运营商名称
     * @return 机房选项列表
     */
    @GetMapping("/options/operator/{operator}")
    @Operation(summary = "根据运营商查询机房选项", description = "根据指定运营商获取机房选项列表")
    @ApiOperationLog(description = "根据运营商查询机房选项")
    public Response<List<ComputerRoomOptionRspVO>> getComputerRoomOptionsByOperator(
            @Parameter(description = "运营商名称", required = true)
            @PathVariable("operator") String operator) {
        List<ComputerRoomOptionRspVO> result = computerRoomService.getComputerRoomOptionsByOperator(operator);
        return Response.success(result);
    }

    /**
     * 获取机房统计信息
     * 
     * @return 机房统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取机房统计信息", description = "获取机房的各项统计数据")
    @ApiOperationLog(description = "获取机房统计信息")
    public Response<ComputerRoomStatisticsRspVO> getComputerRoomStatistics() {
        ComputerRoomStatisticsRspVO result = computerRoomService.getComputerRoomStatistics();
        return Response.success(result);
    }

    /**
     * 检查机房名称是否已存在
     * 
     * @param name 机房名称
     * @param excludeId 排除的机房ID（可选）
     * @return 是否存在
     */
    @GetMapping("/check-name")
    @Operation(summary = "检查机房名称", description = "检查机房名称是否已存在")
    @ApiOperationLog(description = "检查机房名称")
    public Response<Boolean> checkComputerRoomNameExists(
            @Parameter(description = "机房名称", required = true)
            @RequestParam("name") String name,
            @Parameter(description = "排除的机房ID")
            @RequestParam(value = "excludeId", required = false) Long excludeId) {
        Boolean result = computerRoomService.checkComputerRoomNameExists(name, excludeId);
        return Response.success(result);
    }

    /**
     * 根据名称模糊查询机房
     * 
     * @param name 机房名称关键字
     * @return 机房列表
     */
    @GetMapping("/search")
    @Operation(summary = "搜索机房", description = "根据机房名称关键字模糊查询机房")
    @ApiOperationLog(description = "搜索机房")
    public Response<List<ComputerRoomRspVO>> searchComputerRoomByName(
            @Parameter(description = "机房名称关键字", required = true)
            @RequestParam("name") String name) {
        List<ComputerRoomRspVO> result = computerRoomService.searchComputerRoomByName(name);
        return Response.success(result);
    }

    /**
     * 获取所有城市列表
     * 
     * @return 城市列表
     */
    @GetMapping("/cities")
    @Operation(summary = "获取城市列表", description = "获取所有机房所在的城市列表")
    @ApiOperationLog(description = "获取城市列表")
    public Response<List<String>> getAllCities() {
        List<String> result = computerRoomService.getAllCities();
        return Response.success(result);
    }

    /**
     * 获取所有运营商列表
     * 
     * @return 运营商列表
     */
    @GetMapping("/operators")
    @Operation(summary = "获取运营商列表", description = "获取所有机房的运营商列表")
    @ApiOperationLog(description = "获取运营商列表")
    public Response<List<String>> getAllOperators() {
        List<String> result = computerRoomService.getAllOperators();
        return Response.success(result);
    }
}